package com.ruoyi.web.controller.gxs;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.gxs.domain.GxsPendActivity;
import com.ruoyi.gxs.service.IGxsPendActivityService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gxs.domain.GxsJion;
import com.ruoyi.gxs.service.IGxsJionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 参加活动Controller
 * 
 * @author 高晓松
 * @date 2023-12-30
 */
@RestController
@RequestMapping("/gxs/jion")
public class GxsJionController extends BaseController
{
    @Autowired
    private IGxsJionService gxsJionService;

    @Autowired
    private IGxsPendActivityService gxsPendActivityService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询参加活动列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxsJion gxsJion)
    {
        startPage();
//        Long deptId = getDeptId();
//        gxsJion.setDeptId(deptId);
        List<GxsJion> list = gxsJionService.selectGxsJionList(gxsJion);

//        Long parentId = sysDeptService.selectDeptById(deptId).getParentId();
//        gxsJion.setDeptId(parentId);
//        List<GxsJion> listClass = gxsJionService.selectGxsJionList(gxsJion);
//
//        Long parentSchool = sysDeptService.selectDeptById(parentId).getParentId();
//        gxsJion.setDeptId(parentSchool);
//        List<GxsJion> listSchool = gxsJionService.selectGxsJionList(gxsJion);
//
//        listSchool.addAll(listClass);
//        listSchool.addAll(listStudent);

        return getDataTable(list);
    }

    /**
     * 导出参加活动列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:export')")
    @Log(title = "参加活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxsJion gxsJion)
    {
        List<GxsJion> list = gxsJionService.selectGxsJionList(gxsJion);
        ExcelUtil<GxsJion> util = new ExcelUtil<GxsJion>(GxsJion.class);
        util.exportExcel(response, list, "参加活动数据");
    }

    /**
     * 获取参加活动详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:query')")
    @GetMapping(value = "/{gxsJoinid}")
    public AjaxResult getInfo(@PathVariable("gxsJoinid") Long gxsJoinid)
    {
        return success(gxsJionService.selectGxsJionByGxsJoinid(gxsJoinid));
    }

    /**
     * 新增参加活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:add')")
    @Log(title = "参加活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxsJion gxsJion)
    {
        return toAjax(gxsJionService.insertGxsJion(gxsJion));
    }

    /**
     * 修改参加活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:edit')")
    @Log(title = "参加活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxsJion gxsJion)
    {
        return toAjax(gxsJionService.updateGxsJion(gxsJion));
    }

    /**
     * 删除参加活动
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:remove')")
    @Log(title = "参加活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gxsJoinids}")
    public AjaxResult remove(@PathVariable Long[] gxsJoinids)
    {
        return toAjax(gxsJionService.deleteGxsJionByGxsJoinids(gxsJoinids));
    }

    /**
     * 新增参加活动pro
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:add')")
    @Log(title = "参加活动", businessType = BusinessType.INSERT)
    @GetMapping("addJionPro/{gxsPendActivityid}")
    public AjaxResult addJionPro(@PathVariable("gxsPendActivityid") Long gxsPendActivityid)
    {
        //根据选择的活动编号查询活动相关信息 并赋给 gxsJion
        GxsPendActivity gxsPendActivity = gxsPendActivityService.selectGxsPendActivityByGxsPendActivityid(gxsPendActivityid);
        //判断是否还有活动名额
        if (gxsPendActivity.getGxsParticipant() > 0 ) {
            //根据登录信息查询相关信息 并赋给 gxsJion
            SysUser sysUser = sysUserService.selectUserById(getUserId());

            GxsJion gxsJion = new GxsJion();
            gxsJion.setUserId(sysUser.getUserId());
            gxsJion.setUserName(sysUser.getUserName());
            gxsJion.setNickName(sysUser.getNickName());
            gxsJion.setDeptId(sysUser.getDeptId());

            gxsJion.setGxsPendActivityid(gxsPendActivityid);
            gxsJion.setGxsPendTitle(gxsPendActivity.getGxsPendTitle());
            gxsJion.setGxsTypeId(gxsPendActivity.getGxsTypeId());
            gxsJion.setGxsPendState(gxsPendActivity.getGxsPendState());
            gxsJion.setGxsScore(gxsPendActivity.getGxsScore());

            //设置参加状态和加分状态初始状态
            gxsJion.setGxsIfJion(0L);
            gxsJion.setGxsIfAwarded(0L);

            //将报名的活动剩余名额-1
            gxsPendActivity.setGxsParticipant(gxsPendActivity.getGxsParticipant() - 1L);
            gxsPendActivityService.updateGxsPendActivity(gxsPendActivity);

            return toAjax(gxsJionService.insertGxsJion(gxsJion));
        }
        else {
            return AjaxResult.error("当前活动没有名额");
        }
    }


    /**
     * 新增参加活动pro
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:add')")
    @Log(title = "参加活动", businessType = BusinessType.INSERT)
    @GetMapping("applicationn/{gxsPendActivityid}")
    public TableDataInfo applicationn(@PathVariable("gxsPendActivityid") Long gxsPendActivityid)
    {
        GxsJion gxsJion = new GxsJion();
        gxsJion.setGxsPendActivityid(gxsPendActivityid);
        List<GxsJion> list = gxsJionService.selectGxsJionList(gxsJion);

        return getDataTable(list);
    }


    /**
     * 申请活动加分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:remove')")
    @Log(title = "申请活动加分", businessType = BusinessType.DELETE)
    @GetMapping("applyBonus/{gxsJoinids}")
    public AjaxResult applyBonus(@PathVariable Long[] gxsJoinids)
    {
        return toAjax(gxsJionService.applyBonusByGxsJoinids(gxsJoinids));
    }

    /**
     * 参加活动
     */
    @Log(title = "申请活动加分", businessType = BusinessType.DELETE)
    @GetMapping("getApplyJion/{gxsJoinid}")
    public AjaxResult getApplyJion(@PathVariable Long gxsJoinid)
    {
        GxsJion gxsJion = gxsJionService.selectGxsJionByGxsJoinid(gxsJoinid);
        gxsJion.setGxsIfJion(1L);
        return toAjax(gxsJionService.updateGxsJion(gxsJion));
    }


    /**
     * 申请活动加分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:remove')")
    @Log(title = "活动加分", businessType = BusinessType.DELETE)
    @GetMapping("activityBonus/{gxsJoinids}")
    public AjaxResult activityBonus(@PathVariable Long[] gxsJoinids)
    {
        return toAjax(gxsJionService.applyBonusByGxsJoinids(gxsJoinids));
    }



    /**
     * 查看已申请的加分列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:list')")
    @GetMapping("/examineList")
    public TableDataInfo addlist(GxsJion gxsJion)
    {
        startPage();
        List<GxsJion> list = gxsJionService.selectexamineList(gxsJion);
        return getDataTable(list);
    }
    /**
     * 审批待加分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:jion:list')")
    @GetMapping("/bonus/{gxsJoinids}")
    public AjaxResult bonus(@PathVariable Long[] gxsJoinids)
    {
        startPage();
        return toAjax(gxsJionService.activityBonusByGxsJoinids(gxsJoinids));
    }

}
